package workqueue;

import com.rabbitmq.client.*;
import utils.RabbitMQUtils;

import java.io.IOException;

/**
 * @author by Jak
 * @date 2020/7/7
 */
public class Consumer2 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtils.getConnection();
        final Channel channel = connection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare("work", true, false, false, null);

        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override //最后一个参数：消息队列中取出的消息
            public void handleDelivery (String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body) throws IOException {
                System.out.println("消费者-2: " + new String(body));
                // 手动确认 参数1：手动确认消息标识 参数2：false每次确认一个，确认后，队列中就会移除消息
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
